﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XChipTool.Enties;

namespace XChip_Common
{
    public class Port
    {

        public static Dictionary<string, byte> ControlValues = new Dictionary<string, byte>()
        {
            {"SOH", 0x01},
            {"STX", 0x02},
            {"EOT", 0x04},
            {"ACK", 0x06},
            {"NAK" ,0x15},
            {"CAN", 0x18},
            {"CTRLZ", 0x1A},
            {"C", 0x43 }
        };


        public static List<TreeListValue> GetPorts()
        {
            List<TreeListValue> list = new List<TreeListValue>();
            string[] ports = SerialPort.GetPortNames();
            foreach (var item in ports)
            {
                list.Add(new TreeListValue() { ValueMember = item, DisplayMember = item });
            }
            return list;
        }

        public static List<TreeListValue> GetSerialPorts() {
            var ports = SerialPortFinder.GetSerialPorts();
            List<TreeListValue> list = new List<TreeListValue>();
            foreach (var item in ports)
            {
                list.Add(new TreeListValue() { ValueMember = item[1], DisplayMember = item[0]  });
            }
            return list;
        }

        public static List<TreeListValue> GetRate()
        {

            List<TreeListValue> list = new List<TreeListValue>();
            //list.Add(new TreeListValue() { ValueMember = "4800", DisplayMember = "4800" });
            list.Add(new TreeListValue() { ValueMember = "9600", DisplayMember = "*****" });
            list.Add(new TreeListValue() { ValueMember = "19200", DisplayMember = "*****" });
            list.Add(new TreeListValue() { ValueMember = "38400", DisplayMember = "*****" });
            list.Add(new TreeListValue() { ValueMember = "57600", DisplayMember = "*****" });
            list.Add(new TreeListValue() { ValueMember = "115200", DisplayMember = "*****" });
            list.Add(new TreeListValue() { ValueMember = "128000", DisplayMember = "*****" });
            return list;
        }

        public static List<TreeListValue> GetParity()
        {

            List<TreeListValue> list = new List<TreeListValue>();
            list.Add(new TreeListValue() { ValueMember = "0", DisplayMember = "None" });
            list.Add(new TreeListValue() { ValueMember = "1", DisplayMember = "Odd" });
            list.Add(new TreeListValue() { ValueMember = "2", DisplayMember = "Even" });
            list.Add(new TreeListValue() { ValueMember = "3", DisplayMember = "Mark" });
            list.Add(new TreeListValue() { ValueMember = "4", DisplayMember = "Space" });

            return list;
        }
        /// <summary>
        /// 系统频率
        /// </summary>
        /// <returns></returns>
        public static List<TreeListValue> GetFSYS()
        {

            List<TreeListValue> list = new List<TreeListValue>();
            list.Add(new TreeListValue() { ValueMember = "27", DisplayMember = "27" });
            list.Add(new TreeListValue() { ValueMember = "30", DisplayMember = "30" });
            list.Add(new TreeListValue() { ValueMember = "36", DisplayMember = "36" });
            list.Add(new TreeListValue() { ValueMember = "37.125", DisplayMember = "37.125" });
            list.Add(new TreeListValue() { ValueMember = "67.5", DisplayMember = "67.5" });
            list.Add(new TreeListValue() { ValueMember = "74.25", DisplayMember = "74.25" });
            list.Add(new TreeListValue() { ValueMember = "72", DisplayMember = "72" });
            list.Add(new TreeListValue() { ValueMember = "81", DisplayMember = "81" });
            return list;
        }

        /// <summary>
        /// 协议
        /// </summary>
        /// <returns></returns>
        public static List<TreeListValue> GetAgreement()
        {

            List<TreeListValue> list = new List<TreeListValue>();
            list.Add(new TreeListValue() { ValueMember = "0", DisplayMember = "1k-XModem" });
            list.Add(new TreeListValue() { ValueMember = "1", DisplayMember = "XModem" });
            return list;
        }

        public static List<TreeListValue> GetDataBits()
        {
            List<TreeListValue> list = new List<TreeListValue>();
            list.Add(new TreeListValue() { ValueMember = "8", DisplayMember = "8" });
            return list;
        }

        public static List<TreeListValue> GetStopBits()
        {
            List<TreeListValue> list = new List<TreeListValue>();
            list.Add(new TreeListValue() { ValueMember = "1", DisplayMember = "1" });
            return list;
        }



        private static SerialPort _selectedPort;
        /// <summary>
        /// 串口
        /// </summary>
        public static SerialPort SelectedPort
        {
            get
            {
                return _selectedPort;
            }

            set
            {
                _selectedPort = value;
            }
        }

        //public Port(string portName, int baudRate,int dataBits, Parity parity)
        //{
        //    SelectedPort = new SerialPort
        //    {
        //        PortName = portName, //获取或设置通信端口，包括但不限于所有可用的 COM 端口
        //        BaudRate = baudRate, //获取或设置串行波特率
        //        DataBits = dataBits, // 获取或设置每个字节的标准数据位长度
        //        Parity = parity, //获取或设置奇偶校验检查协议
        //        StopBits = StopBits.One, //获取或设置每个字节的标准停止位数
        //        DtrEnable = true, //获取或设置一个值，该值在串行通信过程中启用数据终端就绪 (DTR) 信号
        //        RtsEnable = true //获取或设置一个值，该值指示在串行通信中是否启用请求发送 (RTS) 信号
        //    };
        //}

        public Port() { }
        /// <summary>
        /// 打开串口
        /// </summary>
        public static void Open()
        {
            SelectedPort.Open();
        }
        /// <summary>
        /// 关闭串口
        /// </summary>
        public static void Close()
        {
            SelectedPort.DiscardInBuffer();//清理输入缓冲区
            SelectedPort.DiscardOutBuffer();//清理输出缓冲区
            SelectedPort.Close();
        }

        /// <summary>
        /// 读串口
        /// </summary>
        /// <returns></returns>
        public static byte Read()
        {
            return (byte)SelectedPort.ReadByte();
        }
        /// <summary>
        /// 写串口
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset">补偿</param>
        /// <param name="count">计数</param>
        public static void Write(byte[] buffer, int offset, int count)
        {
            SelectedPort.Write(buffer, offset, count);
        }

        public static void WriteCharacter(byte character)
        {
            var bytes = new byte[1]
            {
                character
            };
            Write(bytes, 0, 1);
        }

        //public short GetCrcSum(byte[] data)
        //{
        //    //short result = 0;
        //    //foreach (var b in data)
        //    //{
        //    //    result = (short)((result << 8) ^ CRC._crc16LookupTable[((result >> 8) ^ b) & 0x00FF]);
        //    //}

        //    //return result;
        //}

        public static byte GetCkSum(byte[] data)
        {
            byte result = 0;
            foreach (var b in data)
            {
                result += b;
            }

            return result;
        }

    }
}
